package com.work.formaldehyde.homefragment;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.work.formaldehyde.R;
import com.work.formaldehyde.activity.FanKuiActivity;
import com.work.formaldehyde.activity.KeFuActivity;
import com.work.formaldehyde.activity.KePuListActivity;
import com.work.formaldehyde.activity.LoginActivity;
import com.work.formaldehyde.activity.OrderActivity;
import com.work.formaldehyde.activity.QuXiaoDingDanActivity;
import com.work.formaldehyde.activity.SetActivity;
import com.work.formaldehyde.activity.SetZhiFuBaoActivity;
import com.work.formaldehyde.activity.ShangPingActivity;
import com.work.formaldehyde.activity.TuiHuoListActivity;
import com.work.formaldehyde.activity.YinSiActivity;
import com.work.formaldehyde.activity.YongHuXieYiActivity;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.util.Url;
import com.work.formaldehyde.view.RoundImageView;

public class WodeFragment extends Fragment implements View.OnClickListener {
    //用于获取视图
    private View root;
    private LinearLayout yjfk, tj, pj, kf, bb, aldd, yhxy, yszc;
    private RelativeLayout xqtitle, daizhifu, daipingjia, daishouhuo, tuihuo, dianjidenglu;
    private TextView username;
    private ImageView jiance_comeback, home_title_share;
    private RoundImageView user_headimg;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_wode, container, false);//初始加载
        if (ApiUtils.isNetworkConnected(getActivity()))
            InitView(root);
        return root;
    }

    public static int sid;

    @Override
    public void onStart() {
        super.onStart();
        if (ApiUtils.isNetworkConnected(getActivity())) {
            if (!Url.USER_HEAD_IMG.equals("")) {
                dianjidenglu.setVisibility(View.GONE);
                user_headimg.setVisibility(View.VISIBLE);
                username.setVisibility(View.VISIBLE);
                ApiUtils.SetLog("Url.USER_HEAD_IMG=" + Url.USER_HEAD_IMG);
                Glide.with(getActivity())
                        .load(Url.USER_HEAD_IMG)
                        .into(user_headimg);
                username.setText(Url.USER_NAME);
            } else {
                dianjidenglu.setVisibility(View.VISIBLE);
                user_headimg.setVisibility(View.GONE);
                username.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 初始加载视图
     */
    public void InitView(View view) {
        dianjidenglu = view.findViewById(R.id.dianjidenglu);
        dianjidenglu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Url.USER_ID.equals("")) {
                    Intent is_login = new Intent(getActivity(), LoginActivity.class);
                    startActivity(is_login, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                }
            }
        });
        tuihuo = view.findViewById(R.id.tuihuo);
        tuihuo.setOnClickListener(this);
        aldd = view.findViewById(R.id.aldd);
        aldd.setOnClickListener(this);
        user_headimg = view.findViewById(R.id.user_headimg);
        username = view.findViewById(R.id.username);
        jiance_comeback = view.findViewById(R.id.jiance_comeback);
        jiance_comeback.setOnClickListener(this);
        daizhifu = view.findViewById(R.id.daizhifu);
        daipingjia = view.findViewById(R.id.daipingjia);
        daishouhuo = view.findViewById(R.id.daishouhuo);
        home_title_share = view.findViewById(R.id.home_title_share);
        daizhifu.setOnClickListener(this);
        daipingjia.setOnClickListener(this);
        daishouhuo.setOnClickListener(this);
        yhxy = view.findViewById(R.id.yhxx);
        yszc = view.findViewById(R.id.yszc);
        yhxy.setOnClickListener(this);
        yszc .setOnClickListener(this);
        yjfk = view.findViewById(R.id.yjfk);
        tj = view.findViewById(R.id.tj);
        pj = view.findViewById(R.id.pj);
        kf = view.findViewById(R.id.kf);
        bb = view.findViewById(R.id.bb);
        home_title_share.setOnClickListener(this);
        yjfk.setOnClickListener(this);
        tj.setOnClickListener(this);
        pj.setOnClickListener(this);
        kf.setOnClickListener(this);
        bb.setOnClickListener(this);
        if (Url.USER_ID.equals("")) {
            dianjidenglu.setVisibility(View.VISIBLE);
            user_headimg.setVisibility(View.GONE);
        } else {
            dianjidenglu.setVisibility(View.GONE);
            user_headimg.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.yszc:
                Intent yszc = new Intent(getActivity(), YinSiActivity.class);
                startActivity(yszc, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.yhxx:
                Intent yhxy = new Intent(getActivity(), YongHuXieYiActivity.class);
                startActivity(yhxy, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.yjfk:
                Intent tofk = new Intent(getActivity(), FanKuiActivity.class);
                startActivity(tofk, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.aldd:
                if (Url.USER_ID.equals("")) {
                    Intent is_login = new Intent(getActivity(), LoginActivity.class);
                    startActivity(is_login, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                    ApiUtils.SetToast(getActivity(), "请先登录");
                } else
                    intent_to_orderactivity(3);
                break;
            case R.id.home_title_share:
                UMImage image1 = new UMImage(getActivity(), Url.FENXIANG_IMG);
                UMWeb web1 = new UMWeb("https://sj.qq.com/myapp/detail.htm?apkName=com.work.formaldehyde");
                web1.setTitle(Url.FENXIANG_TITLE);//标题
                web1.setThumb(image1);  //缩略图
                web1.setDescription("优质生活，从甲醛测试开始");//描述
                new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.WEIXIN).withText(Url.FENXIANG_TITLE)
                        .withMedia(web1)
                        .share();
                break;
            case R.id.tj:
                UMImage image = new UMImage(getActivity(), Url.FENXIANG_IMG);
                UMWeb web = new UMWeb("https://sj.qq.com/myapp/detail.htm?apkName=com.work.formaldehyde");
                web.setTitle(Url.FENXIANG_TITLE);//标题
                web.setThumb(image);  //缩略图
                web.setDescription("优质生活，从甲醛测试开始");//描述
                new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.WEIXIN).withText(Url.FENXIANG_TITLE)
                        .withMedia(web)
                        .share();
                break;
            case R.id.pj:

                break;
            case R.id.kf:
                Intent tokf = new Intent(getActivity(), KeFuActivity.class);
                startActivity(tokf, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.bb:

                break;
            case R.id.tuihuo:
                if (Url.USER_ID.equals("")) {
                    Intent is_login = new Intent(getActivity(), LoginActivity.class);
                    startActivity(is_login, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                    ApiUtils.SetToast(getActivity(), "请先登录");
                } else {
                    Intent qxdd = new Intent(getActivity(), TuiHuoListActivity.class);
                    startActivity(qxdd, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                }
                break;
            case R.id.daizhifu:
                if (Url.USER_ID.equals("")) {
                    Intent is_login = new Intent(getActivity(), LoginActivity.class);
                    startActivity(is_login, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                    ApiUtils.SetToast(getActivity(), "请先登录");
                } else
                    intent_to_orderactivity(0);
                break;
            case R.id.daishouhuo:
                if (Url.USER_ID.equals("")) {
                    Intent is_login = new Intent(getActivity(), LoginActivity.class);
                    startActivity(is_login, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                    ApiUtils.SetToast(getActivity(), "请先登录");
                } else
                    intent_to_orderactivity(1);
                break;
            case R.id.daipingjia:
                if (Url.USER_ID.equals("")) {
                    Intent is_login = new Intent(getActivity(), LoginActivity.class);
                    startActivity(is_login, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                    ApiUtils.SetToast(getActivity(), "请先登录");
                } else
                    intent_to_orderactivity(2);
                break;
            case R.id.jiance_comeback:
                Intent toset = new Intent(getActivity(), SetActivity.class);
                startActivity(toset, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
        }
    }

    /**
     * 用int来判断是什么入口进入的0：待支付 1：待收货 2：待评价 3：全部订单
     *
     * @param id
     */
    public void intent_to_orderactivity(int id) {
        sid = id;
        Intent order = new Intent(getActivity(), OrderActivity.class);
        startActivity(order, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
    }

}